from sklearn.datasets import make_blobs
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans

# 生成模拟图像数据（300个像素点，3种颜色）
X, _ = make_blobs(n_samples=300, centers=3, n_features=2, cluster_std=1.0, random_state=42)

# 执行K-means聚类
kmeans = KMeans(n_clusters=3, random_state=42)
labels = kmeans.fit_predict(X)

# 可视化聚类结果
plt.scatter(X[:,0], X[:,1], c=labels, cmap='viridis', s=50)
plt.scatter(kmeans.cluster_centers_[:,0], kmeans.cluster_centers_[:,1], c='red', s=200, marker='X')
plt.title('Image Segmentation using K-means')
plt.show()